package com.intelligent.controller;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.pojo.Warehouse;
import com.intelligent.service.WarehouseService;
import com.intelligent.utils.Result;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
	@Resource
	private WarehouseService warehouseService;

	/**
	 * 分页查询仓库（联查负责人用户名）
	 */
	@GetMapping("/page")
	public Result<IPage<Warehouse>> page(@RequestParam(defaultValue = "1") long pageNo,
	                                    @RequestParam(defaultValue = "10") long pageSize,
	                                    @RequestParam(required = false) String warehouseCode,
	                                    @RequestParam(required = false) String warehouseName,
	                                    @RequestParam(required = false) String username,
	                                    @RequestParam(required = false) Integer status) {
		Page<Warehouse> page = new Page<>(pageNo, pageSize);
		IPage<Warehouse> data = warehouseService.pageWarehouse(page, warehouseCode, warehouseName, username, status);
		return Result.success(data);
	}

	/**
	 * 根据ID查询仓库（联查负责人用户名）
	 */
	@GetMapping("/{id}")
	public Result<Warehouse> getById(@PathVariable("id") String id) {
		Warehouse data = warehouseService.getByIdWithUser(id);
		return Result.success(data);
	}

	/**
	 * 添加仓库
	 */
	@PostMapping("/add")
	public Result<Warehouse> add(@RequestBody Warehouse warehouse) {
		Warehouse data = warehouseService.addWarehouse(warehouse);
		return Result.success(data);
	}

	/**
	 * 修改仓库
	 */
	@PutMapping("/update/{id}")
	public Result<Warehouse> update(@PathVariable("id") String id, @RequestBody Warehouse warehouse) {
		warehouse.setWarehouseId(Long.valueOf(id));
		Warehouse data = warehouseService.updateWarehouse(warehouse);
		return Result.success(data);
	}

	/**
	 * 删除仓库
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> delete(@PathVariable("id") String id) {
		warehouseService.deleteWarehouse(id);
		return Result.success("删除成功");
	}

}
